import { apiDeleteRelation, apiDelete, getList, apiAdd, apiUpdate } from '@/api/shop/tags'
import tagsApi from '@/api/shop/tags'
import { getToken } from '@/utils/auth'
import { Loading } from 'element-ui'
import { getApiUrl } from '@/utils/utils'
import permission from '@/directive/permission/index.js'
export default {
  directives: { permission },
  data() {
    return {
      apiUrl: getApiUrl(),
      uploadUrl: '',
      uploadFileId: '',
      uploadHeaders: {
        'Authorization': ''
      },
      loadingInstance: {},
      formVisible: false,
      formTitle: '添加banner',
      deptList: [],
      isAdd: true,
      options: [
        { label: '首页', value: 'index' },
        { label: '新闻', value: 'news' },
        { label: '产品', value: 'product' },
        { label: '解决方案', value: 'solution' },
        { label: '案例', value: 'case' }
      ],
      form: {
        id: '',
        title: '',
        page: '',
        param: '',
        img: '',
        idFile: '',
        type: 'index'
      },
      listQuery: {
        title: undefined
      },
      list: null,
      listLoading: true,
      shopTagSelected: {
        id: undefined,
        show: false,
        disabled: true
      },
      attrKey: {
        idCategory: '',
        goodsList: {},
        visible: false,
      },
      attrVal: {
        idAttrKey: '',
        goodsList: {},
        visible: false,
      }
    }
  },
  computed: {
    rules() {
      return {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.uploadUrl = getApiUrl() + '/file'
      this.uploadHeaders['Authorization'] = getToken()
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        //console.log('标签列表',response);
        this.list = response.data.records
        for (var index in this.list) {
          let item = this.list[index]
          item.img = getApiUrl() + '/file/getImgStream?idFile=' + item.idFile
        }
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    reset() {
      this.listQuery.title = ''
      this.listQuery.page = 1
      this.fetchData()
    },
    handleFilter() {
      this.getList()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      if (this.shopTagSelected && currentRow) {
        this.shopTagSelected.id = currentRow.id
      }
    },
    addTagForm() {
      this.form = {
        name: '',
        recommend: 0
      }
    },
    add() {
      this.addTagForm()
      this.formTitle = '添加标签'
      this.formVisible = true
      this.isAdd = true
    },

    save() {
      if (this.isAdd) {
        this.addSave()
      } else {
        this.updateSave()
      }
    },
    addSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          apiAdd({
            name: this.form.name,
            recommend: this.form.recommend,
          }).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
            this.formVisible = false
          })
        } else {
          return false
        }
      })
    },
    update(data) {
      if (this.checkSel()) {
        //console.log("更新",this.shopTagSelected);
        this.form = {
          id: this.shopTagSelected.id,
          name: this.shopTagSelected.name,
          recommend: this.shopTagSelected.recommend
        }
        if (this.shopTagSelected.recommend === "1") {
          this.form.recommend = 1
        }else{
          this.form.recommend = 0
        }
        this.formTitle = '修改标签'
        this.formVisible = true
        this.isAdd = false
      }
    },
    checkSel() {
      if (this.shopTagSelected && this.shopTagSelected.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    clickRow(row) {
      this.shopTagSelected.disabled = false
      this.shopTagSelected.id = row.id
      this.shopTagSelected.name = row.name
      this.shopTagSelected.recommend = row.recommend
    },
    updateSave() {
      if (this.checkSel()) {
        //console.log(3333, this.form.recommend)
        apiUpdate({
          id: this.form.id,
          name: this.form.name,
          recommend: this.form.recommend,
        }).then(response => {
          //console.log('更新标签', response)
          this.$message({
            message: this.$t('common.optionSuccess'),
            type: 'success'
          })
          this.fetchData()
          this.formVisible = false
        })
      }
    },

    remove() {
      if (this.checkSel()) {
        var id = this.shopTagSelected.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
          apiDelete(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          })
        }).catch(() => {
        })
      }
    },
    goodsManagementButton(goodsList) {
      this.attrKey.visible = true
      this.attrKey.goodsList = goodsList
    },
    goodsManagementDeleteGood(goodId) {
      apiDeleteRelation(goodId).then(response => {
        this.$message({
          message: this.$t('common.optionSuccess'),
          type: 'success'
        })
        this.fetchData()
        this.attrKey.visible = true
        this.attrKey.goodsList = goodsList
      })
    },
  }
}
